from Dialog import *
from login_ui import *
from urllib_page import *
from loading import *
import sys
import threading #导入threading包


class login:
    
    def __init__(self):
        app = QtWidgets.QApplication(sys.argv)
        self.dialog = Dialog();
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("pl.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.dialog.setWindowIcon(icon)
        
        try:
            fileOpen = open('login.ini')
            self.serverUrl = fileOpen.read()
            fileOpen.close()
        except Exception as err:
            self.dialog.infoMsg('获取地址失败')
            exit()
        self.url = urllib_page()
        
        movie = QMovie("ajax-loader.gif")
        splash = MovieSplashScreen(movie)
        splash.show()
        
        self.loading = {'status':False}
        
        threads = []
        t1 = threading.Thread(target=self.checkComputer)
        threads.append(t1)
        
        threads[0].setDaemon(True)
        threads[0].start()
        while(self.loading['status'] == False):
            app.processEvents()
        else:
            splash.hide()
            if(self.loading['data'] == False):
                self.dialog.infoMsg('请求异常')
                exit()
            else:
                if(str(self.loading['data']['status']) != '200'):
                    self.dialog.infoMsg(self.loading['data']['msg'])
                    exit()
        
        self._Dialog = QtWidgets.QDialog()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self._Dialog)
        try:
            self.ui.registerComboBox(self._Dialog, self.loading['data']['data'])
        except Exception as err:
            self.dialog.infoMsg('获取网站列表失败')
            exit()
        
        self._Dialog.show()
        
        self.ui.pushButton.clicked.connect(self.login)
        self.ui.pushButton_2.clicked.connect(self.reset)
        
        sys.exit(app.exec_())
        
    #验证计算机信息
    def checkComputer(self):
        import getpass
        from uuid import getnode as get_mac
        
        user = getpass.getuser()
        mac = ':'.join(("%012X" % get_mac())[i:i + 2] for i in range(0, 12, 2))
        
        checkMacAddress = self.serverUrl+"?g=Api&m=Check&a=Mac";

        data = {'mac': mac, 'user':user}
        
        result = self.url.getUrl(checkMacAddress, data, returnJson=True)
        self.loading = {'status':True, 'data':result};
        
    #执行登录
    def login(self):
        import html
        account = str(self.ui.lineEdit.text())
        passwd = str(self.ui.lineEdit_2.text())
        apiLogin = str(html.unescape(self.ui.comboBox.itemData(self.ui.comboBox.currentIndex())))
        
        if(account == ''):
            self.dialog.infoMsg("请填写帐号")
            return False
        elif(passwd == ''):
            self.dialog.infoMsg("请填写密码")
            return False;

        data = {'account': account, 'password':passwd}
        
        loginResult = self.url.getUrl(apiLogin, data, returnJson=True)
        if(loginResult == False):
            self.dialog.infoMsg('请求异常')
            return False
        elif(str(loginResult['status']) == '200'):
            import webbrowser
            webbrowser.open(str(loginResult['data']))
            self._Dialog.close()
        else:
            self.dialog.infoMsg(loginResult['msg'])
            self.ui.lineEdit_2.setText("")
            return False
        
    #重置输入框
    def reset(self):
        self.ui.lineEdit.setText("")
        self.ui.lineEdit_2.setText("")
        


if __name__ == "__main__":
    swaroop = login()
